package com.example.stock.service.impl;

import com.example.stock.excel.DailyDeSum;
import com.example.stock.service.DailyDeSumService;
import com.example.stock.util.DataUtil;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DailyDeSumServiceImpl implements DailyDeSumService {

    private List<DailyDeSum> dailyDeSumList() {
        return DataUtil.getDailyDeSumData();
    }

    @PostConstruct
    private void init() {
        dailyDeSumGroupByStock = dailyDeSumList().stream().collect(Collectors.groupingBy(DailyDeSum::getStock));
    }

    private Map<String, List<DailyDeSum>> dailyDeSumGroupByStock;

    @Override
    public Map<String, List<DailyDeSum>> getDailyDeSumGroupByStock() {
        return dailyDeSumGroupByStock;
    }

    @Override
    public List<DailyDeSum> getDailyDeSumByStock(String stock) {
        return getDailyDeSumGroupByStock().get(stock);
    }

    @Override
    public List<DailyDeSum> getLatestDailyDeSum() {
        return getLatestDailyDeSum(200);
    }

    @Override
    public List<DailyDeSum> getLatestDailyDeSum(int count) {
        return dailyDeSumList().stream()
                .sorted(Comparator.comparing(DailyDeSum::getDate).reversed())
                .limit(count).collect(Collectors.toList());
    }
}